package com.clz.appdesign.myapp.download;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.clz.appdesign.appdependence.net.download.FileCallBack;
import com.clz.appdesign.appdependence.net.download.FileSubscriber;
import com.clz.appdesign.appdependence.net.download.RetrofitDownLoad;
import com.clz.appdesign.appdependence.net.updatebyrx2.UpdateManager;
import com.clz.appdesign.myapp.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;


public class DownloadActivity extends AppCompatActivity {

    @BindView(R.id.pb_test)
    ProgressBar mPbTest;
    String fileName = "app.apk";
    String fileStoreDir = Environment.getExternalStorageDirectory().getAbsolutePath();
    private static final String URL = "http://download.fir.im/v2/app/install/5818acbcca87a836f50014af?download_token=a01301d7f6f8f4957643c3fcfe5ba6ff";
    private static final String UPDATE_URL = "http://imtt.dd.qq.com/16891/905F0A4FAEEFCD0746F596CB9A02B61D.apk";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download);
        ButterKnife.bind(this);
        testUpdate();
    }

    private void testUpdate() {
        UpdateManager.getInstance().showUpdateDialog(UPDATE_URL,DownloadActivity.this);
    }

    public void testDownload() {
        final FileCallBack<ResponseBody> callBack = new FileCallBack<ResponseBody>(fileStoreDir, fileName) {

            @Override
            public void onSuccess(final ResponseBody responseBody) {
                Toast.makeText(DownloadActivity.this, "下载文件成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void progress(long progress, long total) {
                LogUtils.dTag("download2", progress + "");
                mPbTest.setProgress((int) ((double) progress / total * 100));
            }

            @Override
            public void onstart() {

            }


            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }
        };
        RetrofitDownLoad.getInstence().API()
                .download(URL)
                .subscribeOn(Schedulers.io())//请求网络 在调度者的io线程
                .observeOn(Schedulers.io()) //指定线程保存文件
                .doOnNext(new Action1<ResponseBody>() {
                    @Override
                    public void call(ResponseBody body) {
                        callBack.saveFile(body);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread()) //在主线程中更新ui
                .subscribe(new FileSubscriber<ResponseBody>(callBack));

    }
}

